#include"Stack.h"

void InitStack(Stack* s)
{
	assert(s);
	s->a = malloc(sizeof(Stack) * 4);
	if (s->a == NULL)
	{
		printf("malloc_failed");
		exit(-1);
	}
	s->capacity = 4;
	s->top = 0;
}
void DestroySTack(Stack* s)
{
	assert(s);
	free(s->a);
	s->a = NULL;
	s->capacity = s->top = 0;
}
void StackPush(Stack* s, STDATATYPE x)
{
	if (s->top == s->capacity)
	{
		Stack* tmp = realloc(s->a, sizeof(Stack) * s->capacity * 2);
		if (s->a == NULL)
		{
			printf("realloc_failed");
			exit(-1);
		}
		else
		{
			s->a = tmp;
			s->capacity *= 2;
		}
	}
	s->a[s->top] = x;
	s->top++;
}
void StackPop(Stack* s)
{
	assert(s);
	assert(s->top > 0);
	s->top--;
}
STDATATYPE StackTop(Stack* s)
{
	assert(s);
	assert(s->top > 0);
	return s->a[s->top-1];
}
int StackSIze(Stack* s)
{
	assert(s);
	return s->top;
}
bool StackIsEmpty(Stack* s)
{
	assert(s);
	return s->top == 0;
}

